package com.epam.command.logic;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.epam.dao.DAOFactory;
import com.epam.dao.DAOFactory.DAOType;
import com.epam.dao.exception.TourNotAvailableException;
import com.epam.dao.impl.PurchaseDAO;
import com.epam.dao.intf.ITouristVoucherDAO;
import com.epam.entity.Purchase;

public class BuyLogic {
	

	public static void buy(int userId, int voucherId) throws TourNotAvailableException {
		
		
		String date = getCurrentDate();		
		ITouristVoucherDAO tvd = (ITouristVoucherDAO) DAOFactory.INSTANCE.getDAO(DAOType.TOURISTVOUCHER);
		PurchaseDAO purchaseDAO = new PurchaseDAO();
		
		Purchase purchase = new Purchase.PurchaseBuilder().userId(userId).voucherId(voucherId).date(date).build();
	
		purchaseDAO.insert(purchase);
		tvd.updateAvailability(voucherId, false);
		
	
	
	} 

	private static String getCurrentDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		sdf.format(date);
		return date.toString();
	}
	
}
